package com.hencoderplus.demo1;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathDashPathEffect;
import android.graphics.PathMeasure;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * hencoderplus
 * Created by lzw on 2018/10/23. 15:50:59
 * 邮箱：632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */
public class DashBoard extends View {

    private static final int LEN = (int) Utils.dp2Px(100);
    private static final int LENGTH = (int) Utils.dp2Px(80);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private static final float startAngle = 30f;
    private static final float sweepAngle = 240f;
    public static final int ANGEL = 120;
    private int advance = 50;
    Path dash = new Path();
    PathDashPathEffect effect;


    RectF rectF = new RectF();
    {
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(Utils.dp2Px(2));
        dash.addRect(0,0,Utils.dp2Px(2),Utils.dp2Px(10), Path.Direction.CW);

        Path arc = new Path();
        arc.addArc(getWidth()/2-LEN,getHeight()/2-LEN,getWidth()/2+LEN,getHeight()/2+LEN,90+ANGEL/2,360-ANGEL);
        PathMeasure pathMeasure = new PathMeasure(arc,false);
        effect = new PathDashPathEffect(dash,pathMeasure.getLength()/20-Utils.dp2Px(2),0,PathDashPathEffect.Style.ROTATE);


    }

    public DashBoard(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        rectF.set(getWidth()/2-LEN,getHeight()/2-LEN,getWidth()/2+LEN,getHeight()/2+LEN);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画线
        canvas.drawArc(rectF,90+ANGEL/2,360-ANGEL,false,paint);

        //画刻度
        paint.setPathEffect(effect);
        canvas.drawArc(rectF,90+ANGEL/2,360-ANGEL,false,paint);
        paint.setPathEffect(null);

        //画指针
        canvas.drawLine(getWidth()/2,getHeight()/2,
                (float) Math.cos(Math.toRadians(getAngelFromMask(15)))*LENGTH+getWidth()/2,
                (float) Math.sin(Math.toRadians(getAngelFromMask(15)))*LENGTH+getHeight()/2
                , paint);

    }


    private double getAngelFromMask(int i) {
        return 90+ANGEL/2+(360-ANGEL)/20*i;
    }
}
